//
//  VisitorView.swift
//  SwiftStory
//
//  Created by ZxjMacBookPro on 2020/4/16.
//  Copyright © 2020 ZxjMacBookPro. All rights reserved.
//

import UIKit

protocol VisitorViewDelegate : NSObjectProtocol{
    func visitorViewDidClickLoginBtn(visitorView: VisitorView)
    func visitorViewDidClickRegisterBtn(visitorView: VisitorView)
}

typealias ClickLoginBlock = (_ visitorView: VisitorView) -> ()
typealias ClickRegisterBlock = (_ visitorView: VisitorView) -> ()


class VisitorView: UIView {
    var loginBlock = ClickLoginBlock.self
    var registerBlock = ClickRegisterBlock?.self

    @IBOutlet weak var rotationView: UIImageView!
    
    @IBOutlet weak var iconImageView: UIImageView!
    
    @IBOutlet weak var registerBtn: UIButton!
    
    @IBOutlet weak var loginBtn: UIButton!
    
    @IBOutlet weak var titileLB: UILabel!
    
    class func visitorView() -> VisitorView{
        return Bundle.main.loadNibNamed("VisitorView", owner: nil, options:nil)?.last as! VisitorView
    }
    
    func setupVisitorInfo(imageName: String?, title: String) -> Void {
        titileLB.text = title

        guard let name = imageName else {
            startAniamtion()
            return
        }
        rotationView.isHidden = true
        //不是首页
        iconImageView.image = UIImage(named: name)
    }
    
    private func startAniamtion(){
        //1.创建动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //2.设置动画
        anim.toValue = 2 * Double.pi
        anim.duration = 5.0
        anim.repeatCount = MAXFLOAT
        //默认情况视图消失，系统自动移除动画
        anim.isRemovedOnCompletion = false
        
        //3.添加动画
        rotationView.layer.add(anim, forKey: nil)
    }
}
